<template>
  <div>
    <div style="height: 60px; display: flex; align-items: center; border-bottom: 1px solid #ddd">
      <div style="flex: 1">
        <div style="padding-left: 20px; display: flex; align-items: center">
          <img src="@/assets/imgs/logo.png" alt="" style="width: 60px">
          <div style="font-weight: bold; font-size: 24px; margin-left: 5px">宠物援助系统</div>
        </div>
      </div>
      <div style="width: fit-content; padding-right: 10px; display: flex; align-items: center;">
        <el-dropdown @command="handleCommand">
      <span class="el-dropdown-link">
        <img
            style="width: 40px; height: 40px; border-radius: 50%"
            :src="data.user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'"
            alt="avatar"
        />
      </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item  @click="goToProfile" ><span>个人资料</span> </el-dropdown-item>
              <el-dropdown-item  @click="changePassword" ><span>修改密码</span> </el-dropdown-item>
              <el-dropdown-item  @click="logout" ><span>退出登录</span> </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <span style="margin-left: 5px">{{ data.user.name }}</span>
      </div>
    </div>

    <div style="display: flex">
      <div style="width: 200px; border-right: 1px solid #ddd; min-height: calc(100vh - 60px)">
        <el-menu
            router
            style="border: none"
            :default-active="router.currentRoute.value.path"
            :default-openeds="['/home', '2','3']"
        >
          <el-menu-item index="/home">
            <el-icon><HomeFilled /></el-icon>
            <span>系统首页</span>
          </el-menu-item>
          <el-sub-menu index="3">
            <template #title>
              <el-icon><Promotion /></el-icon>
              <span>信息管理</span>
            </template>

            <el-menu-item index="/notice" v-if="data.user.role === 'ADMIN'" > <el-icon><Platform /></el-icon><span>公告信息</span></el-menu-item>
            <el-menu-item index="/city" > <el-icon><OfficeBuilding /></el-icon><span>组织信息</span></el-menu-item>
            <el-menu-item index="/animal" > <el-icon><Sunny /></el-icon><span>组织宠物信息</span></el-menu-item>
            <el-menu-item index="/help" > <el-icon><HelpFilled /></el-icon> <span>救援信息</span></el-menu-item>
            <el-menu-item index="/helper" > <el-icon><Aim /></el-icon> <span>已选择的援助信息</span></el-menu-item>
            <el-menu-item index="/adopt" > <el-icon><StarFilled /></el-icon> <span>领养信息</span></el-menu-item>


          </el-sub-menu>

          <el-sub-menu index="2" v-if="data.user.role === 'ADMIN'">
            <template #title>
             <el-icon><Avatar /></el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/admin"> <el-icon><User /></el-icon><span>管理员信息</span></el-menu-item>
            <el-menu-item index="/volunteer"> <el-icon><User /></el-icon><span>志愿者信息</span></el-menu-item>
            <el-menu-item index="/user"> <el-icon><User /></el-icon><span>用户信息</span></el-menu-item>
            <el-menu-item index="/apply"> <el-icon><BellFilled /></el-icon><span>申请信息</span></el-menu-item>
          </el-sub-menu>
          <el-menu-item  v-if="data.user.role === 'ADMIN'" index="/person"><el-icon><User /></el-icon><span>个人资料</span></el-menu-item>
          <el-menu-item  v-if="data.user.role === 'VOLUNTEER'" index="/vperson"><el-icon><User /></el-icon><span>个人资料</span></el-menu-item>
          <el-menu-item v-if="data.user.role === 'USER'" index="/uperson"><el-icon><User /></el-icon><span>个人资料</span></el-menu-item>
          <el-menu-item index="/password">
            <el-icon><Lock /></el-icon>
            <span>修改密码</span>
          </el-menu-item>
          <el-menu-item index="login" @click="logout">
            <el-icon><SwitchButton /></el-icon>
            <span>退出系统</span>
          </el-menu-item>
        </el-menu>
      </div>

      <div style="flex: 1; width: 0; background-color: #f8f8ff; padding: 10px">
        <router-view @updateUser="updateUser" />
      </div>
    </div>

  </div>
</template>

<script setup>
import { reactive } from "vue";
import router from "@/router";
import {ElMessage} from "element-plus";
import {useRouter} from 'vue-router';

const router1 = useRouter()

const data = reactive({
  user: JSON.parse(localStorage.getItem('system-user') || '{}')
})

if (!data.user?.id) {
  ElMessage.error('请登录！')
  router.push('/login')
}

const updateUser = () => {
  data.user = JSON.parse(localStorage.getItem('system-user') || '{}')
}

const logout = () => {
  ElMessage.success('退出成功')
  localStorage.removeItem('system-user')
  router.push('/login')
}


  function  handleClick() {
      console.log('头像被点击');
      // 这里可以加入其他点击头像时的逻辑
    }
function goToProfile() {
      // 跳转到个人资料页面
  if(data.user.role == 'ADMIN'){
    router1.push({path: '/person'});
  }
  else if(data.user.role == 'USER'){
    router1.push({path: '/UPerson'});
  }
  else if(data.user.role == 'VOLUNTEER'){
    router1.push({path: '/VPerson'});
  }
    }
function changePassword() {
      // 跳转到修改密码页面
     router1.push({ path: '/password' });
    }


</script>

<style scoped>
.el-menu-item.is-active {
  background-color: #e0edfd !important;
}
.el-menu-item:hover {
  color: #1967e3;
}

th {
  color: #333;
}

</style>